clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
use permno permco datem ret1-ret60 mcap LTG using $Work/ibes_ret, clear
keep if dofm(datem)>=mdy(12,1,1981)

merge 1:1 permno permco datem using $Work/EBR_new_monthly, keepusing(permco permno datem statpers ret_mo1-ret_mo60 qnic1-qnic60 bemeLTM inv op L12ret BOOK SI10 INV MOME PROF)
keep if _merge==3
rename SI10 SIZE
keep if BOOK<.
tabulate _merge 
drop _merge
keep if dofm(datem)>=mdy(12,1,1981)
* **************************
save $Temp/AppendixB1, replace
* **************************
* Add dividend-to-price ratio (msf_adjusted.dta generated by read_crsp.do)
use $Work/msf_adjusted, clear
tsset permno datem
gen  retYr1=1
gen retxYr1=1
forvalues x=0(1)11 {
	replace   retYr1= retYr1*(1+L`x'.ret )
	replace  retxYr1=retxYr1*(1+L`x'.retx)
}
gen dp=retYr1/retxYr1-1
keep permco permno datem dp
gstats winsor dp, cuts(1 99) by(datem) replace  
save $Work/dp, replace
* *******************************
* compute means, count obs, etc
import sas using $Data/CRSP/msf.sas7bdat, clear 
rename *, lower
replace prc=abs(prc)
gen datem=mofd(date)
format datem %tm
keep permco permno datem cfacshr shrout prc

merge 1:1 permco permno datem using $Work/street_mthly 
keep if _merge==3

generate eps=1000*streetLTM/(cfacshr*shrout)

tsset permno datem
generate L0eps=L3.eps
generate L1eps=L12.L0eps
generate L2eps=L24.L0eps

generate   e_p=L0eps/(prc/cfacshr)
generate cae_p=((L0eps+L1eps+L2eps)/3)/(prc/cfacshr)
keep permco permno datem cae_p e_p

tempfile pe
save "`pe'", replace
* ------------------------------------------------- *
merge 1:1 permco permno datem using $Temp/AppendixB1
keep if ret1<.
drop if _merge==1
drop _merge
* ------------------------------------------------- *
merge 1:1 permco permno datem using $Work/dp, keepusing(permco permno datem dp)
drop if _merge==2
drop _merge

foreach var in bemeLTM inv op mcap L12ret ret1 e_p cae_p dp {
	gen `var'KF=`var' 
	gen `var'RU=`var' if qnic1!=.
}
* ------------------------------------------------- *
collapse (mean) *KF *RU (count) obsKF=ret1KF obsRU=ret1RU, by(datem)
drop ret1KF ret1RU
* ------------------------------------------------- *
putexcel set $Tables/AppendixB1_June.xlsx, replace

preserve
tabstat *KF, stat(mean) save
return list
matrix results = r(StatTotal)'
putexcel A2="beme"
putexcel A3="inv"
putexcel A4="op"
putexcel A5="mcap"
putexcel A6="Lagged Ret"
putexcel A7="e_p"
putexcel A8="cae_p"
putexcel A9="dp"
putexcel A10="obs"

putexcel B1="FF"
putexcel B2=matrix(results) 

tabstat *KF, stat(sd) save
return list
matrix results = r(StatTotal)'
putexcel D1="sd FF"
putexcel D2=matrix(results)
* ***********
tabstat *RU, stat(mean) save
return list
matrix results = r(StatTotal)'
putexcel C1="OurSample"
putexcel C2=matrix(results)

tabstat *RU, stat(sd) save
return list
matrix results = r(StatTotal)'
putexcel E1="sd OurSmpl"
putexcel E2=matrix(results)

matrix store = J(9,4,.)
tsset datem
local i=1
foreach var in bemeLTM inv op mcap  L12ret e_p cae_p dp obs {
gen dif=`var'KF-`var'RU
ivreg2  dif, bw(13)
matrix tmp = r(table) 
matrix store[`i',1]=tmp[1..4,.]'
local i=`i'+1
drop dif
}
matrix list store
restore
putexcel F1="difference"
putexcel G1="standard error"
putexcel H1="z-stat"
putexcel I1="significance"

putexcel F2=matrix(store)
